home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / tusrc.zip / SRC / UNIQ.C < prev    next >
C/C++ Source or Header  |  1993-10-02  |  9KB  |  347 lines

  1. /* uniq -- remove duplicate lines from a sorted file
  2.    Copyright (C) 1986, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Richard Stallman and David MacKenzie. */
  19.  
  20. /* Get isblank from GNU libc.  */
  21. #define _GNU_SOURCE
  22.  
  23. #include <stdio.h>
  24. #include "../lib/getopt.h"
  25. #include <sys/types.h>
  26. #include "system.h"
  27. #include "../lib/linebuffer.h"
  28. #include "version.h"
  29.  
  30. // #define min(x, y) ((x) < (y) ? (x) : (y))
  31.  
  32. void error ();
  33.  
  34. static char *find_field ();
  35. static int different ();
  36. static void check_file ();
  37. static void usage ();
  38. static void writeline ();
  39.  
  40. /* The name this program was run with. */
  41. char *program_name;
  42.  
  43. /* Number of fields to skip on each line when doing comparisons. */
  44. static int skip_fields;
  45.  
  46. /* Number of chars to skip after skipping any fields. */
  47. static int skip_chars;
  48.  
  49. /* Number of chars to compare; if 0, compare the whole lines. */
  50. static int check_chars;
  51.  
  52. enum countmode
  53. {
  54.   count_occurrences,        /* -c Print count before output lines. */
  55.   count_none            /* Default.  Do not print counts. */
  56. };
  57.  
  58. /* Whether and how to precede the output lines with a count of the number of
  59.    times they occurred in the input. */
  60. static enum countmode countmode;
  61.  
  62. enum output_mode
  63. {
  64.   output_repeated,        /* -d Only lines that are repeated. */
  65.   output_unique,        /* -u Only lines that are not repeated. */
  66.   output_all            /* Default.  Print first copy of each line. */
  67. };
  68.  
  69. /* Which lines to output. */
  70. static enum output_mode mode;
  71.  
  72. /* If non-zero, display usage information and exit.  */
  73. static int flag_help;
  74.  
  75. /* If non-zero, print the version on standard error.  */
  76. static int flag_version;
  77.  
  78. static struct option const longopts[] =
  79. {
  80.   {"count", no_argument, NULL, 'c'},
  81.   {"repeated", no_argument, NULL, 'd'},
  82.   {"unique", no_argument, NULL, 'u'},
  83.   {"skip-fields", required_argument, NULL, 'f'},
  84.   {"skip-chars", required_argument, NULL, 's'},
  85.   {"check-chars", required_argument, NULL, 'w'},
  86.   {"help", no_argument, &flag_help, 1},
  87.   {"version", no_argument, &flag_version, 1},
  88.   {NULL, 0, NULL, 0}
  89. };
  90.  
  91. void
  92. main (argc, argv)
  93.      int argc;
  94.      char *argv[];
  95. {
  96.   int optc;
  97.   char *infile = "-", *outfile = "-";
  98.  
  99.   program_name = argv[0];
  100.   skip_chars = 0;
  101.   skip_fields = 0;
  102.   check_chars = 0;
  103.   mode = output_all;
  104.   countmode = count_none;
  105.  
  106.   while ((optc = getopt_long (argc, argv, "0123456789cdf:s:uw:", longopts,
  107.                   (int *) 0)) != EOF)
  108.     {
  109.       switch (optc)
  110.     {
  111.     case 0:
  112.       break;
  113.  
  114.     case '0':
  115.     case '1':
  116.     case '2':
  117.     case '3':
  118.     case '4':
  119.     case '5':
  120.     case '6':
  121.     case '7':
  122.     case '8':
  123.     case '9':
  124.       skip_fields = skip_fields * 10 + optc - '0';
  125.       break;
  126.  
  127.     case 'c':
  128.       countmode = count_occurrences;
  129.       break;
  130.  
  131.     case 'd':
  132.       mode = output_repeated;
  133.       break;
  134.  
  135.     case 'f':        /* Like '-#'. */
  136.       skip_fields = atoi (optarg);
  137.       break;
  138.  
  139.     case 's':        /* Like '+#'. */
  140.       skip_chars = atoi (optarg);
  141.       break;
  142.  
  143.     case 'u':
  144.       mode = output_unique;
  145.       break;
  146.       
  147.     case 'w':
  148.       check_chars = atoi (optarg);
  149.       break;
  150.       
  151.     default:
  152.       usage ();
  153.     }
  154.     }
  155.  
  156.   if (flag_version)
  157.     {
  158.       fprintf (stderr, "%s\n", version_string);
  159.       exit (0);
  160.     }
  161.  
  162.   if (flag_help)
  163.     usage ();
  164.  
  165.   if (optind >= 2 && strcmp (argv[optind - 1], "--") != 0)
  166.     {
  167.       /* Interpret non-option arguments with leading `+' only
  168.      if we haven't seen `--'.  */
  169.       while (optind < argc && argv[optind][0] == '+')
  170.     skip_chars = atoi (argv[optind++]);
  171.     }
  172.  
  173.   if (optind < argc)
  174.     infile = argv[optind++];
  175.  
  176.   if (optind < argc)
  177.     outfile = argv[optind++];
  178.  
  179.   if (optind < argc)
  180.     usage ();            /* Extra arguments. */
  181.  
  182.   check_file (infile, outfile);
  183.  
  184.   exit (0);
  185. }
  186.  
  187. /* Process input file INFILE with output to OUTFILE.
  188.    If either is "-", use the standard I/O stream for it instead. */
  189.  
  190. static void
  191. check_file (infile, outfile)
  192.      char *infile, *outfile;
  193. {
  194.   FILE *istream;
  195.   FILE *ostream;
  196.   struct linebuffer lb1, lb2;
  197.   struct linebuffer *thisline, *prevline, *exch;
  198.   char *prevfield, *thisfield;
  199.   int prevlen, thislen;
  200.   int match_count = 0;
  201.  
  202.   if (!strcmp (infile, "-"))
  203.     istream = stdin;
  204.   else
  205.     istream = fopen (infile, "r");
  206.   if (istream == NULL)
  207.     error (1, errno, "%s", infile);
  208.  
  209.   if (!strcmp (outfile, "-"))
  210.     ostream = stdout;
  211.   else
  212.     ostream = fopen (outfile, "w");
  213.   if (ostream == NULL)
  214.     error (1, errno, "%s", outfile);
  215.  
  216.   thisline = &lb1;
  217.   prevline = &lb2;
  218.  
  219.   initbuffer (thisline);
  220.   initbuffer (prevline);
  221.  
  222.   if (readline (prevline, istream) == 0)
  223.     goto closefiles;
  224.   prevfield = find_field (prevline);
  225.   prevlen = prevline->length - (prevfield - prevline->buffer);
  226.  
  227.   while (!feof (istream))
  228.     {
  229.       if (readline (thisline, istream) == 0)
  230.     break;
  231.       thisfield = find_field (thisline);
  232.       thislen = thisline->length - (thisfield - thisline->buffer);
  233.       if (!different (thisfield, prevfield, thislen, prevlen))
  234.     match_count++;
  235.       else
  236.     {
  237.       writeline (prevline, ostream, match_count);
  238.       match_count = 0;
  239.  
  240.       exch = prevline;
  241.       prevline = thisline;
  242.       thisline = exch;
  243.       prevfield = thisfield;
  244.       prevlen = thislen;
  245.     }
  246.     }
  247.  
  248.   writeline (prevline, ostream, match_count);
  249.  
  250.  closefiles:
  251.   if (ferror (istream) || fclose (istream) == EOF)
  252.     error (1, errno, "error reading %s", infile);
  253.  
  254.   if (ferror (ostream) || fclose (ostream) == EOF)
  255.     error (1, errno, "error writing %s", outfile);
  256.  
  257.   free (lb1.buffer);
  258.   free (lb2.buffer);
  259. }
  260.  
  261. /* Given a linebuffer LINE,
  262.    return a pointer to the beginning of the line's field to be compared. */
  263.  
  264. static char *
  265. find_field (line)
  266.      struct linebuffer *line;
  267. {
  268.   register int count;
  269.   register char *lp = line->buffer;
  270.   register int size = line->length;
  271.   register int i = 0;
  272.  
  273.   for (count = 0; count < skip_fields && i < size; count++)
  274.     {
  275.       while (i < size && ISBLANK (lp[i]))
  276.     i++;
  277.       while (i < size && !ISBLANK (lp[i]))
  278.     i++;
  279.     }
  280.  
  281.   for (count = 0; count < skip_chars && i < size; count++)
  282.     i++;
  283.  
  284.   return lp + i;
  285. }
  286.  
  287. /* Return zero if two strings OLD and NEW match, nonzero if not.
  288.    OLD and NEW point not to the beginnings of the lines
  289.    but rather to the beginnings of the fields to compare.
  290.    OLDLEN and NEWLEN are their lengths. */
  291.  
  292. static int
  293. different (old, new, oldlen, newlen)
  294.      char *old;
  295.      char *new;
  296.      int oldlen;
  297.      int newlen;
  298. {
  299.   register int order;
  300.  
  301.   if (check_chars)
  302.     {
  303.       if (oldlen > check_chars)
  304.     oldlen = check_chars;
  305.       if (newlen > check_chars)
  306.     newlen = check_chars;
  307.     }
  308.   order = memcmp (old, new, min (oldlen, newlen));
  309.   if (order == 0)
  310.     return oldlen - newlen;
  311.   return order;
  312. }
  313.  
  314. /* Output the line in linebuffer LINE to stream STREAM
  315.    provided that the switches say it should be output.
  316.    If requested, print the number of times it occurred, as well;
  317.    LINECOUNT + 1 is the number of times that the line occurred. */
  318.  
  319. static void
  320. writeline (line, stream, linecount)
  321.      struct linebuffer *line;
  322.      FILE *stream;
  323.      int linecount;
  324. {
  325.   if ((mode == output_unique && linecount != 0)
  326.       || (mode == output_repeated && linecount == 0))
  327.     return;
  328.  
  329.   if (countmode == count_occurrences)
  330.     fprintf (stream, "%7d\t", linecount + 1);
  331.  
  332.   fwrite (line->buffer, sizeof (char), line->length, stream);
  333.   putc ('\n', stream);
  334. }
  335.  
  336. static void
  337. usage ()
  338. {
  339.   fprintf (stderr, "\
  340. Usage: %s [-cdu] [-f skip-fields] [-s skip-chars] [-w check-chars]\n\
  341.        [-#skip-fields] [+#skip-chars] [--count] [--repeated] [--unique]\n\
  342.        [--skip-fields=skip-fields] [--skip-chars=skip-chars]\n\
  343.        [--check-chars=check-chars] [--help] [--version] [infile] [outfile]\n",
  344.        program_name);
  345.   exit (1);
  346. }
  347.